﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{

    public class AB55Data : BaseFunction
    {
        
        public AB55Data()
            : base()
        {

        }
        public AB55Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string KRS ,LFD ;
            KRS = dwABPOS["KRS"].ToString();
            LFD = dwABPOS["LFD"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "SELECT TRIM(E1.LSNAME1) WFACTORY,TRIM(E1.LSAPNA1) AS STAFF,E.ABPANRKR,E.ABPALFDNR, "
                    + " E.ABPAUFKRS||'-'||TO_NUMBER(E.ABPAUFLFD)||'-'||TO_NUMBER(E.ABPAUFPOS) AS WORDERNO, "
                    + " TRIM(E.ABPART)||'-'||TRIM(E.ABPFARBE)||'-'||E.ABPVAR AS ARTICLE, "
                    + " A.ABPAUFKRS||'-'||TO_NUMBER(A.ABPAUFLFD)||'-'||TO_NUMBER(A.ABPAUFPOS) AS SORDERNO, "
                    + " TRIM(B.LSNAME1) AS SFACTORY,D.ABASANZSTK*D.ABASSTKLG*0.001 AS SMTR, "
                    + " D.ABASANZSTK AS SPCNUM,D.ABASSTKLG*0.001 AS SQTY,A.ABPENDDAT,G.WAKEAFAKTR*0.001 AS RATE, "
                    + " ARWERT*0.001 AS PRICE,T.WTGFDISM*0.01 AS FDISM  "
                    + " FROM ABPOS A,(SELECT DISTINCT ABFMANDANT,ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,ABFLIEFNR,C.LSNAME1 "
                    + " FROM ABFOLGE B,LIEFDAT C WHERE B.ABFMANDANT = C.LSMANDANT AND B.ABFLIEFNR = C.LSLIEFNR  "
                    + " AND TRIM(ABFLIEFNR) IS NOT NULL) B, "
                    + " ABAUFM D,ABPOS E,(SELECT DISTINCT ABFMANDANT,ABFAUFKRS,ABFAUFLFD,ABFAUFPOS, "
                    + " ABFLIEFNR,LSNAME1,LSAPNA1 "
                    + " FROM ABFOLGE E1,LIEFDAT E2 WHERE E1.ABFMANDANT = E2.LSMANDANT AND E1.ABFLIEFNR = E2.LSLIEFNR "
                    + "  AND TRIM(ABFLIEFNR) IS NOT NULL) E1,ABPR F,WASTKE G,WTGEW T "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT AND A.ABPAUFKRS = B.ABFAUFKRS "
                    + " AND A.ABPAUFLFD = B.ABFAUFLFD AND A.ABPAUFPOS = B.ABFAUFPOS "
                    + " AND A.ABPMANDANT = D.ABAMANDANT AND A.ABPAUFKRS = D.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = D.ABAAUFLFD AND A.ABPAUFPOS = D.ABAAUFPOS "
                    + " AND D.ABAAPOS >'0001' AND D.ABAMANDANT = E.ABPMANDANT "
                    + " AND D.ABAVKAUFKRS = E.ABPAUFKRS AND D.ABAVKAUFLFD = E.ABPAUFLFD "
                    + " AND D.ABAVKAUFPOS = E.ABPAUFPOS "
                    + " AND E.ABPMANDANT = E1.ABFMANDANT AND E.ABPAUFKRS = E1.ABFAUFKRS "
                    + " AND E.ABPAUFLFD = E1.ABFAUFLFD AND E.ABPAUFPOS = E1.ABFAUFPOS "
                    + " AND E.ABPMANDANT = F.ARMANDANT AND E.ABPAUFKRS = F.ARAUFKRS "
                    + " AND E.ABPAUFLFD = F.ARAUFLFD AND E.ABPAUFPOS = F.ARAUFPOS "
                    + " AND F.ARAFOLGE ='000' AND E.ABPMANDANT = G.WAKMANDANT AND E.ABPART = G.WAKART "
                    + " AND TRIM(E.ABPVAR) = G.WAKVAR AND E.ABPFARBE = G.WAKWCOL AND G.WAKPOS = '01' "
                    + " AND E.ABPMANDANT = T.WTGMANDANT AND E.ABPKUNDNR = T.WTGKUNDNR "
                    + " AND E.ABPWERK = T.WTGWERK AND E.ABPABT = T.WTGABT  "
                    + " AND E.ABPART = T.WTGART AND TRIM(E.ABPVAR) = T.WTGVAR "
                    + " AND A.ABPAUFART = 'VW' AND A.ABPGESSTA <> 'SP' "
                    + " AND E.ABPAUFART = 'DI' AND E.ABPGESSTA = 'SP' "
                    + " AND E.ABPANRKR = '" + KRS + "' AND E.ABPALFDNR = '" + LFD + "' "
                 ;
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in setLocal.Tables["ABPOS"].Rows)
                    {
                        ColumnName = "WFACTORY";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "STAFF";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "SFACTORY";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                    }

                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        
        public DataSet BuildABPOS(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }

    }
}